import { GetAllTestList, AddTestList, ReviseTestList,NewTestBar, GetTestBar, DeleteTestList,DeleteTestBar, ReviseTestBar, GetSomeTestList, GetTestList} from '@/api/test'

const getDefaultState = () => {
    return {
        testlist: [],
        testbar: [],
        maxpage: ''
    }
}
const state = getDefaultState()
const mutations = {
    //存取题目
    SET_testlist: (state,testlist) => {
        state.testlist = testlist
    },
    //存取题单
    SET_testbar: (state, testbar) => {
        state.testbar = testbar
    },
    // 获取题目最大页数
    SET_maxpage: (state, maxpage) => {
        state.maxpage = maxpage
    }
}

const actions = {
    //获取所有题目  分页查询
    async getAllTestList({commit},page) {
        try {
            let result = await GetAllTestList(page)
            const res = result.data
            if (res.msg === "获取成功") {
                commit('SET_testlist', res.data)
                commit('SET_maxpage',res.max_page)
                return "ok"
            } else {
                return new Promise((reslove, reject) => {
                    reject('fail');
                })
            }
        } catch (error) {
            console.log(error)
            return new Promise((reslove, reject) => {
                reject('fail');
            })
      }
    },
    async getTestList({ commit }) {
        let result = await GetTestList()
        const res = result.data
        if (res.msg === "获取成功") {
            commit('SET_testlist', res.data)
            return "ok"
        }
    },
    //获取特定题目
    // async getSomeTestList({ commit }, search) {
    //     try {
    //         let result = await GetAllTestList(search)
    //         const res = result.data
    //         if (res.msg === "获取成功") {
    //             commit('SET_testlist', res.data)
    //             return "ok"
    //         } else {
    //             return new Promise((reslove, reject) => {
    //                 reject('fail');
    //             })
    //         }
    //     } catch (error) {
    //         console.log(error)
    //         return new Promise((reslove, reject) => {
    //             reject('fail');
    //         })
    //   }
    // },
    //添加题目
    async newTestList({commit},testinfo) {
        const { topics, options,answer,effect } = testinfo
        // console.log(testinfo)
        // console.log(topics)
        let res = await AddTestList({topics, options,answer,effect})
        if (res.status == 200) {
            return "ok"
        }
    },
    //修改题目
    async reviseTestList({commit},data) {
        let res = await ReviseTestList(data)
        if (res.status == 200) {
            return "ok"
        }
    },
    // 删除题目
    async deleteTestList({ commit }, uuid) {
        let res = await DeleteTestList(uuid)
        if (res.status == 200) {
            return "ok"
        }  
    },
    //获取题单
    async getTestBar({commit}) {
        let result = await GetTestBar()
        const res = result.data
        if (res.msg == "获取成功") {
            commit("SET_testbar", res.data)
            return "ok"
        }
    },     
    //新建题单
    async newTestBar({commit},testbar) {
        const { name, list, topics,standard,scoring_criteria } = testbar
        let res = await NewTestBar({name, list, topics,standard,scoring_criteria})
        if (res.status == 200) {
            return "ok"
        }
    },
    // 删除题单
    async deleteTestBar({ commit }, uuid) {
        let res = await DeleteTestBar(uuid)
        if (res.status == 200) {
            return "ok"
        }
    },
    //修改题单
    async reviseTestBar({ commit },data) {
        let res = await ReviseTestBar(data)
        // console.log(res)
        if (res.status == 200) {
            return "ok"
        }
    }

}

export default {
    state,
    mutations,
    actions

}